﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using WPDataServiceDemo.Models;
using WPDataServiceDemo.Queries;

namespace WPDataServiceDemo
{
    /// <summary>
    /// Summary description for Service
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld(string name="")
        {
            return "Hello World: " + name; ;
        }

        [WebMethod]
        public List<supplier> GetListSuppliers()
        {
            var data = GetData.getListSupplier();
            return data.ToList();
        }

        [WebMethod]
        public List<supplier> SearcSuppliersByName(String searchkey)
        {
            var data = GetData.searchSupplierByName(searchkey);
            return data.ToList();
        }

        [WebMethod]
        public List<menu> SearchMenuByAddressAndName(String tenmon,String diachi)
        {

            var data = GetData.SearchMenuByAddressAndName(tenmon, diachi); 
            return data.ToList();
        }

        [WebMethod]
        public supplier GetSupplierById(int id)
        {
            var data = GetData.getSupplierById(id);
            return data;
        }

        [WebMethod]
        public List<menu> GetMenusBySupplierId(int id)
        {
            var data = GetData.getListMenuBySupplierId(id);
            return data.ToList();
        }

          [WebMethod]
        public List<menu> SearchMenusByName(String name)
        {
            var data = GetData.searchMenuByName(name);
            return data.ToList();
        }
        [WebMethod]
        public menu GetMenuById(int id)
        {
            var data = GetData.getMenuById(id);
            return data;
        }
          [WebMethod]
        public List<menu> GetMenusByListId(int[] ids)
        {
            var data = GetData.getMenusByListId(ids);
            return data.ToList();
        }

          [WebMethod]
          public bool Booking(String dataString,String diachi,String sodt,String hoten)
          {
              try
              {
                  var data = new Dictionary<int, int>();
                  if (dataString.Length > 3)
                  {
                      var dataParse = dataString.Split(',');
                      if (dataParse.Count() > 0)
                      {
                          foreach (String dataS in dataParse)
                          {
                              var dataItem = dataS.Split(':');
                              if (dataItem.Count() == 2)
                              {
                                  data[int.Parse(dataItem[0].ToString())] = int.Parse(dataItem[1].ToString());
                              }
                          }
                      }
                  }

                  if (data.Count > 0)
                  {
                      decimal price = 0;
                      var listDetail = new List<order_detail>();
                      foreach (int id in data.Keys)
                      {
                          var menu = GetData.getMenuById(id);
                          if (menu != null)
                          {
                              price += menu.price;
                              order_detail detail = new order_detail();
                              detail.menu_id = id;
                              detail.amount = data[id];
                              detail.modified_date = DateTime.Now.ToString();
                              listDetail.Add(detail);
                          }
                      }
                      var order = new order();
                      order.buyer = hoten;
                      order.phone = sodt;
                      order.shipping_address = diachi;
                      order.shipping_date = DateTime.Now.ToString();
                      order.total_price = price;
                      order.status = 1;
                      order.order_date = DateTime.Now.ToString();
                      order.modified_date = DateTime.Now.ToString();
                      GetData.Booking(order, (IEnumerable<order_detail>)listDetail);
                      return true;
                  }
              }
              catch (Exception ex)
              {
                  return false;
              }
              return false;
          }
       
       
    }
}
